package com.fourforfo.fourmall.product.web;

import com.fourforfo.common.utils.HttpUtils;
import com.fourforfo.fourmall.product.entity.CategoryEntity;
import com.fourforfo.fourmall.product.service.CategoryService;
import com.fourforfo.fourmall.product.vo.Catalog2VO;
import com.xxl.sso.core.conf.Conf;
import com.xxl.sso.core.user.XxlSsoUser;
import org.apache.ibatis.annotations.Param;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 网站首页映射
 */
@Controller
public class IndexController {

    @Autowired
    RedissonClient redissonClient;

    @Autowired
    CategoryService categoryService;

    @Autowired
    StringRedisTemplate redisTemplate;

    /**
     *
     * @param model 用于封装页面所需数据
     * @return
     */
    @GetMapping({"/","/index.html"})
    public String indexPage(Model model, HttpServletRequest request, RedirectAttributes attributes){


        String url = HttpUtils.getRequestURLByForwordedHost(request);
        model.addAttribute(Conf.REDIRECT_URL,url);

        //1. 查出一级分类
        List<CategoryEntity> categoryEntities = categoryService.getLevel1();

        //视图解析器根据前后缀进行拼串
        //DEFAULT_PREFIX = "classpath:/templates/";
        //DEFAULT_SUFFIX = ".html
        //实际路径：classpath:/templates/+返回值+.html
        model.addAttribute("categories",categoryEntities);
        return "index";
    }

    /**
     *
     * @param model 获取二级三级分类并封装到Catalog2VO
     * @return
     * @uri //index/catalog.json
     */
    @ResponseBody
    @GetMapping({"/index/catalog.json"})
    public Map<String,List<Catalog2VO>> getCatalogJson(Model model){

        //封装分类数据
        Map<String,List<Catalog2VO>> map = categoryService.getCatalogJson();

        return map;
    }



}
